package com.zxx.yuanze.ocp;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.zxx.yuanze.ocp.answer.ImageCache;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 开闭原则
 * Open Close Principle
 * @author zqq on 2020/3/13.
 */
public class DiskCache implements ImageCache {

    //（但是SD卡缓存 与 内存缓存 只能用其一）
    private static String cacheDir = "sdcard/cache/";

    //从SD卡里获取缓存
    public Bitmap get(String url){
        return BitmapFactory.decodeFile(cacheDir + url);
    }

    //将图片缓存到SD卡中
    public void  put(String url,Bitmap bmp){
        FileOutputStream stream = null;

        try {
            stream = new FileOutputStream(cacheDir + url);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
